package club.hicode.daydayup.jackson;

import club.hicode.daydayup.feign.vo.StudentVo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.jsonzou.jmockdata.JMockData;
import org.junit.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Jackson的测试
 *
 * @author Liu Chunfu
 * @date 2018-07-10 上午10:58
 **/
public class JackTest {

    String str = "[{\"id\":\"kgq56k\",\"name\":\"9Y8hIRM\",\"gender\":6097,\"bookVoList\":[{\"bookId\":\"gcoO\",\"name\":\"CSfTckxD\",\"price\":4546.616037844339},{\"bookId\":\"Qi4Yjon\",\"name\":\"Xw8ft4K\",\"price\":5539.2697377439135},{\"bookId\":\"8Ro1m\",\"name\":\"lLqm3\",\"price\":3712.3062358190127}]},{\"id\":\"9p3xj87\",\"name\":\"kPxE\",\"gender\":9937,\"bookVoList\":[{\"bookId\":\"NkRsHnzE\",\"name\":\"qrZJ\",\"price\":7526.409623931949},{\"bookId\":\"uHydGR\",\"name\":\"J9M\",\"price\":2717.5206196891845},{\"bookId\":\"pW\",\"name\":\"25eEkRhFn\",\"price\":2864.0450448320353},{\"bookId\":\"aVMsfUHI\",\"name\":\"l8L\",\"price\":6546.90860135813}]},{\"id\":\"u\",\"name\":\"MF\",\"gender\":711,\"bookVoList\":[{\"bookId\":\"q71bEIS9c\",\"name\":\"dEb6XC\",\"price\":4855.8074480422}]},{\"id\":\"aTwb7fhc8N\",\"name\":\"1FekUGc\",\"gender\":123,\"bookVoList\":[{\"bookId\":\"HOccKriPj6\",\"name\":\"GZOlaNU\",\"price\":9539.000394659273},{\"bookId\":\"sjYFvg\",\"name\":\"gSf3IeGtgZ\",\"price\":3266.911003108427},{\"bookId\":\"RCuJv\",\"name\":\"1xxzC9W7\",\"price\":6706.523061690829},{\"bookId\":\"7Po6Rv\",\"name\":\"h\",\"price\":8571.558199217541},{\"bookId\":\"Uq\",\"name\":\"Zn\",\"price\":8770.42505134047}]},{\"id\":\"cem\",\"name\":\"50m\",\"gender\":4792,\"bookVoList\":[{\"bookId\":\"5TMtqWgPQ\",\"name\":\"P\",\"price\":9752.239283794566},{\"bookId\":\"vXmJ\",\"name\":\"CylMIuJ\",\"price\":3489.0100154858883},{\"bookId\":\"GEp\",\"name\":\"EPEHFYg\",\"price\":1044.6289225319695},{\"bookId\":\"5\",\"name\":\"u\",\"price\":364.52205668079716},{\"bookId\":\"a\",\"name\":\"A2e3FW0f\",\"price\":6749.227246953359},{\"bookId\":\"1o3pLBQ\",\"name\":\"YaQaq2TuIg\",\"price\":4836.071123415989},{\"bookId\":\"U6\",\"name\":\"7\",\"price\":3803.746830956349}]}]";

    @Test
    public void test1() throws JsonProcessingException {
        List<StudentVo> collect = Stream.generate(() -> JMockData.mock(StudentVo.class)).limit(5).collect(Collectors.toList());
        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(collect);
        System.out.println(s);
    }

    @Test
    public void test2() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        //如果不做措施，默认是通过map进行存储的。
        List list = mapper.readValue(str, List.class);
        System.out.println();
    }

    //通过构造TypeReference来处理，推荐作用
    @Test
    public void test3() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        List<StudentVo> list = mapper.readValue(str, new TypeReference<List<StudentVo>>() {
        });
        list.forEach(System.out::println);
    }

    @Test
    public void test4() throws IOException {
        ObjectMapper mapper = new ObjectMapper();

    }


}
